#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` [-s sample_name -d id] [-p out_prefix] [-b bed] reads1.fq reads2.fq
exit 1
}

while getopts  ":s:d:p:b:" opts
do
    case  $opts  in
        s) sample_name=$OPTARG;;
        d) id_=$OPTARG;;
        p) out_prefix=$OPTARG;;
        b) bed=$OPTARG;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -lt 2 ]; then info; fi


. $var
if test -z "$id_"; then id_=$sample_name; fi
if test -z "$library"; then library=$sample_name; fi
read_group=\
@RG\\tID:${id_}\\tPL:ILLUMINA\\tSM:$sample_name\\tLB:$library

reads_seq_1=$1
reads_seq_2=$2
 
echo; echo; echo start bwa mem ...
$bwa mem -M -t 8 -R $read_group $ref_genome \
$reads_seq_1 \
$reads_seq_2 | samtools view -b -@6 -q20 -F12 -L $bed - > $out_prefix.bam
sort_idx_sam.sh -p$out_prefix $out_prefix.bam
rm $out_prefix.bam*


# echo; echo; echo 'picard sam->bam sort index fixmateinformation'
# java -Xmx$java_memory $tmp -jar ${picard_path}FixMateInformation.jar \
# I=$out_prefix.sam \
# O=$out_prefix.sort.bam \
# SORT_ORDER=coordinate \
# CREATE_INDEX=true

. $cmd_done


